﻿/*
* CommFunc.cs
*
* 功 能： 采集数据转换算法
* 类 名： CommFunc
*
* Ver    变更日期  20131125   负责人  黄永生  变更内容
* ───────────────────────────────────
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OracleClient;
using System.Collections;
using System.ComponentModel;
using Maticsoft.DBUtility;

namespace DQYT.HTDZ.BusinessService
{
    /// <summary>
    /// 公共函数类
    /// </summary>
    public class CommFunc
    {
        /// <summary>
        /// 公共函数构造器
        /// </summary>
        public CommFunc()
        {
            //
            // TODO: 在此处添加构造函数逻辑 
            //
        }

        /// <summary>
        /// 异常字符串过滤解析
        /// </summary>
        /// <param name="p_Msg"></param>
        /// <returns></returns>
        public static bool IsErrorFromMessage(string p_Msg)
        {
            if ((!string.IsNullOrEmpty(p_Msg)) &&
                (p_Msg.Length > 5) &&
                (p_Msg.Substring(0, 5) == "ERROR"))
                return true;
            else
                return false;

        }

        /// <summary>
        /// 把数据库中的日期字段的值，按指定格式转为字符串 
        /// </summary>
        /// <param name="p_RQ">例如：drowJH["WZRQ"]</param>
        /// <param name="p_Format">日期格式</param>
        /// <returns></returns>
        public static string getDateFromDB(object p_RQ, string p_Format)
        {
            string strRQ = "";
            if (!(p_RQ is DBNull))
            {
                DateTime tmpDate = Convert.ToDateTime(p_RQ);
                string strYear = tmpDate.Year.ToString();
                string strMonth = tmpDate.Month.ToString();
                string strDay = tmpDate.Day.ToString();
                if (p_Format == "年月日")
                    strRQ = strYear + "年" + strMonth + "月" + strDay + "日";
            }
            return strRQ;
        }

        /// <summary>
        /// 日期解析
        /// </summary>
        /// <param name="DateTime1"></param>
        /// <param name="DateTime2"></param>
        /// <returns></returns>
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;

            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            dateDiff = ts.Days.ToString() + "天"
                + ts.Hours.ToString() + "小时"
                + ts.Minutes.ToString() + "分钟";
            //  + ts.Seconds.ToString() + "秒";

            return dateDiff;
        }




        /// <summary>
        ///查询两列,根据这两个列的值填充DropDownList
        ///第一列作为TEXT
        ///第二列作为value
        ///sql中也可以是一列
        /// </summary>
        /// <param name="p_ddl"></param>
        /// <param name="p_sql"></param>
        public static void FillDDL(DropDownList p_ddl, string p_sql)
        {
            #region 代码实现
            p_ddl.Items.Clear();
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            int iColumns = dt.Columns.Count;
            foreach (DataRow drow in dt.Rows)
            {
                if (iColumns == 2)
                {
                    li = new ListItem(drow[0].ToString(), drow[1].ToString());
                }
                else if (iColumns == 3)
                {
                    string cn_name = (drow[2] != null && drow[2].ToString().Length != 0) ? drow[2].ToString() : "";

                    string name = drow[0].ToString();

                    if (cn_name.Length != 0 && cn_name != name)
                        name += "(" + cn_name + ")";

                    li = new ListItem(name, drow[1].ToString());
                }
                else
                {
                    li = new ListItem(drow[0].ToString(), drow[0].ToString());
                }
                p_ddl.Items.Add(li);
            }
            #endregion 代码实现
        }

        /// <summary>
        ///查询两列,根据这两个列的值填充DropDownList
        ///第一列作为TEXT
        ///第二列作为value
        ///sql中也可以是一列
        /// </summary>
        /// <param name="p_ddl"></param>
        /// <param name="p_sql"></param>
        public static void FillDDL_NotClear(DropDownList p_ddl, string p_sql)
        {
            #region 代码实现
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            int iColumns = dt.Columns.Count;
            foreach (DataRow drow in dt.Rows)
            {
                if (iColumns == 2)
                {
                    li = new ListItem(drow[0].ToString(), drow[1].ToString());
                }
                else if (iColumns == 3)
                {
                    string cn_name = (drow[2] != null && drow[2].ToString().Length != 0) ? drow[2].ToString() : "";

                    string name = drow[0].ToString();

                    if (cn_name.Length != 0 && cn_name != name)
                        name += "(" + cn_name + ")";

                    li = new ListItem(name, drow[1].ToString());
                }
                else
                {
                    li = new ListItem(drow[0].ToString(), drow[0].ToString());
                }
                p_ddl.Items.Add(li);
            }
            #endregion 代码实现
        }

        /// <summary>
        ///查询两列,根据这两个列的值填充CheckBoxList
        ///第一列作为TEXT
        ///第二列作为value
        /// </summary>
        /// <param name="p_ckl"></param>
        /// <param name="p_sql"></param>
        /// <param name="p_checked"></param>
        public static void FillCheckBoxList(CheckBoxList p_ckl, string p_sql, bool p_checked)
        {
            #region 代码实现
            p_ckl.Items.Clear();
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            foreach (DataRow drow in dt.Rows)
            {
                if (dt.Columns.Count == 2)
                {
                    li = new ListItem(drow[0].ToString(), drow[1].ToString());
                    li.Selected = p_checked;
                    p_ckl.Items.Add(li);
                }
                else if (dt.Columns.Count == 3)
                {

                    //解析出中文名称
                    string cn_name = (drow[2] != null && drow[2].ToString().Length != 0) ? drow[2].ToString() : "";

                    string name = drow[0].ToString();

                    if (cn_name.Length != 0 && cn_name != name)
                        name += "(" + cn_name + ")";

                    li = new ListItem(name, drow[1].ToString());
                    li.Selected = p_checked;
                    p_ckl.Items.Add(li);
                }
            }
            #endregion 代码实现
        }


        /// <summary>
        /// 查询1列,根据这1列的值填充CheckBoxList
        /// </summary>
        /// <param name="p_ckl"></param>
        /// <param name="p_sql"></param>
        /// <param name="p_checked"></param>
        public static void FillCheckBoxList_OneColumn(CheckBoxList p_ckl, string p_sql, bool p_checked)
        {
            #region 代码实现
            p_ckl.Items.Clear();
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            string strValue = "";

            foreach (DataRow drow in dt.Rows)
            {
                strValue = drow[0].ToString();
                if (strValue.Trim().Length > 0)  //空值的不显示
                {
                    li = new ListItem(strValue,
                        strValue);
                    li.Selected = p_checked;
                    p_ckl.Items.Add(li);
                }
            }
            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_ckl"></param>
        /// <param name="p_strList"></param>
        /// <param name="p_checked"></param>
        public static void FillCheckBoxListWithStringList(CheckBoxList p_ckl, string p_strList, bool p_checked)
        {
            #region 代码实现
            p_ckl.Items.Clear();
            ListItem li;
            string strList = p_strList.Replace("'", "");

            string[] split = strList.Split(',');
            foreach (string strValue in split)
            {
                li = new ListItem(strValue, strValue);
                li.Selected = p_checked;
                p_ckl.Items.Add(li);

            }

            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_rbl"></param>
        /// <param name="p_sql"></param>
        public static void FillRadioButtonList_OneColumn(RadioButtonList p_rbl, string p_sql)
        {
            #region 代码实现
            p_rbl.Items.Clear();
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            string strValue = "";

            foreach (DataRow drow in dt.Rows)
            {
                strValue = drow[0].ToString();
                if (strValue.Trim().Length > 0)  //空值的不显示
                {
                    li = new ListItem(strValue,
                        strValue);
                    //li.Selected = p_checked;
                    p_rbl.Items.Add(li);
                }
            }

            if (p_rbl.Items.Count > 0)
                p_rbl.Items[0].Selected = true;
            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_rbl"></param>
        /// <param name="p_strList"></param>
        public static void FillRadioButtonListWithStringList(RadioButtonList p_rbl, string p_strList)
        {
            #region 代码实现
            p_rbl.Items.Clear();
            ListItem li;
            string strList = p_strList.Replace("'", "");

            string[] split = strList.Split(',');
            foreach (string strValue in split)
            {
                li = new ListItem(strValue, strValue);
                p_rbl.Items.Add(li);
            }
            if (p_rbl.Items.Count > 0)
                p_rbl.Items[0].Selected = true;

            #endregion 代码实现
        }

        /// <summary>
        /// 只查询一列,根据这个列的值填充DropDownList
        /// </summary>
        /// <param name="p_ddl"></param>
        /// <param name="p_sql"></param>
        public static void FillDDL_OneColumn(DropDownList p_ddl, string p_sql)
        {
            #region 代码实现
            p_ddl.Items.Clear();
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            foreach (DataRow drow in dt.Rows)
            {
                //字段值是空或为空字符的，都不要填入列表中。
                if (!(drow[0] is DBNull))
                {
                    li = new ListItem(drow[0].ToString(),
                        drow[0].ToString());
                    p_ddl.Items.Add(li);
                }
            }
            #endregion 代码实现
        }


        /// <summary>
        /// 只查询一列,根据这个列的值填充DropDownList
        /// </summary>
        /// <param name="p_ddl"></param>
        /// <param name="p_sql"></param>
        /// <param name="p_DefaultValue"></param>
        public static void FillDDL_OneColumn(DropDownList p_ddl, string p_sql, string p_DefaultValue)
        {
            #region 代码实现
            p_ddl.Items.Clear();
            p_ddl.Items.Add(new ListItem(p_DefaultValue, p_DefaultValue));
            ListItem li;
            DataTable dt = DbHelperOra.Query_Table(p_sql);
            foreach (DataRow drow in dt.Rows)
            {
                //字段值是空或为空字符的，都不要填入列表中。
                if (!(drow[0] is DBNull))
                {
                    li = new ListItem(drow[0].ToString(),
                        drow[0].ToString());
                    p_ddl.Items.Add(li);
                }
            }
            #endregion 代码实现
        }


        /// <summary>
        /// 根据一个文件名取得该文件所在的目录
        /// </summary>
        /// <param name="p_File">文件名（带绝对路径）</param>
        /// <returns>该文件所在目录，型如：c:\temp</returns>
        public static string GetDir(string p_File)
        {
            string strDir = "";
            int iPos = p_File.LastIndexOf(@"\");//最后一个"\"的位置
            if (iPos >= 0)  //的确带路径
                strDir = p_File.Substring(0, iPos);
            else
                strDir = ""; //不带路径
            return strDir;
            //private string GetDir(string p_File)
        }

        /// <summary>
        /// 根据一个文件名取得该文件不带目录的文件名
        /// </summary>
        /// <param name="p_File">文件名（带绝对路径）</param>
        /// <returns>纯文件名，不带路径</returns>
        public static string GetOnlyFileName(string p_File)
        {
            string strFileName = "";
            int iPos = p_File.LastIndexOf(@"\");//最后一个"\"的位置
            if (iPos >= 0)  //的确带路径
                strFileName = p_File.Substring(iPos + 1);
            else
                strFileName = p_File; //不带路径
            return strFileName;
            //private string GetOnlyFileName(string p_File)
        }

        /// <summary>
        /// 根据一个文件名取得该文件不带目录、且不带扩展名的文件名
        /// </summary>
        /// <param name="p_File">形如：d:\wd\aa.doc</param>
        /// <returns>aa</returns>
        public static string GetOnlyFileName_NoExt(string p_File)
        {
            string strFileName = GetOnlyFileName(p_File);
            int iPos = strFileName.LastIndexOf(@".");//最后一个"."的位置
            if (iPos >= 0)  //的确带扩展名
                strFileName = strFileName.Substring(0, iPos);

            return strFileName;
        }

        /// <summary>
        /// 把一个文件复制到另一个文件
        /// </summary>
        /// <param name="p_s_File">源文件名（带绝对路径）</param>
        /// <param name="p_d_File">目的文件名（带绝对路径）</param>
        public static void CopyFile(string p_s_File, string p_d_File)
        {
            #region 代码实现
            string strDir = GetDir(p_d_File);
            if (!Directory.Exists(strDir))
            {
                Directory.CreateDirectory(strDir); //如果该目录不存在，建立该目录
            }

            if (File.Exists(p_d_File))
                try
                {
                    File.Delete(p_d_File);//如果该文件已经存在，删除
                }
                catch
                {
                    //删除文件失败时（例如该文件正在使用中）不做任何操作
                }

            // Copy the file.
            File.Copy(p_s_File, p_d_File);

            #endregion 代码实现
        }


        /// <summary>
        /// 给字符串加密
        /// </summary>
        /// <param name="CodeString">密文</param>
        /// <returns></returns>
        public static string EncryptCode(string strCode)
        {
            byte[] byCode = ASCIIEncoding.ASCII.GetBytes(strCode);
            byte[] byResult = new byte[strCode.Length];

            int j = 0;
            for (int i = 0; i < strCode.Length; i++)
            {
                byResult[i] = (byte)(byCode[i] + 5);
            }
            return ASCIIEncoding.ASCII.GetString(byResult, 0, byResult.Length);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="strCode"></param>
        /// <returns></returns>
        public static string DecryptCode(string strCode)
        {
            byte[] byCode = ASCIIEncoding.ASCII.GetBytes(strCode);
            byte[] byResult = new byte[strCode.Length];

            int j = 0;
            for (int i = 0; i < strCode.Length; i++)
            {
                byResult[i] = (byte)(byCode[i] - 5);
            }
            return ASCIIEncoding.ASCII.GetString(byResult, 0, byResult.Length);
        }

        /// <summary>
        /// 遍历CHECKBOXLIST中的各项，哪个项的VALUE值与传入的参数相同，就返回该项的索引
        /// </summary>
        /// <param name="p_Value">比较值</param>
        /// <param name="p_box">要遍历CHECKBOXLIST</param>
        /// <returns>索引</returns>
        public static int GetBoxIndexByValue(string p_Value, CheckBoxList p_box)
        {
            int iIndex = -1;
            int iCount = p_box.Items.Count;
            for (int i = 0; i < iCount; i++)
            {
                if (p_box.Items[i].Value == p_Value)
                {
                    iIndex = i;
                    break;
                }
            }//for (int i=0;i<iCount;i++)
            return iIndex;
            //private int GetBoxIndexByValue()
        }

        /// <summary>
        /// 遍历dropdownlist中的各项，哪个项的VALUE值与传入的参数相同，就返回该项的索引
        /// </summary>
        /// <param name="p_Value">比较值</param>
        /// <param name="p_ddl">要遍历dropdownLIST</param>
        /// <returns>索引</returns>
        public static int GetIndexByValue(string p_Value, DropDownList p_ddl)
        {
            int iIndex = -1;
            int iCount = p_ddl.Items.Count;
            for (int i = 0; i < iCount; i++)
            {
                if (p_ddl.Items[i].Value == p_Value)
                {
                    iIndex = i;
                    break;
                }
            }//for (int i=0;i<iCount;i++)
            return iIndex;
            //private int GetIndexByValue()
        }

        /// <summary>
        /// 把checkboxlist中被选中的项目的value取出用逗号隔开拼成一个字符串，返回
        /// </summary>
        /// <param name="p_ckBox">checkboxlist列表</param>
        /// <returns>型如：1a,2b,3c,4d </returns>
        public static string GetValueListFromBox(CheckBoxList p_ckBox)
        {
            string strJHID_List = "";

            foreach (ListItem li in p_ckBox.Items)
            {
                if (li.Selected == true)
                {
                    strJHID_List += li.Value + ",";
                }//if (li.Selected )
            }//foreach (ListItem li in p_ckBox.Items )
            if (strJHID_List.Length > 0)
                strJHID_List = strJHID_List.Substring(0, strJHID_List.Length - 1);//去掉末尾多余的一个逗号

            return strJHID_List;
            //private string GetJHIDFromBox
        }

        /// <summary>
        /// 取得checkboxlist中被选中的项目的数量
        /// </summary>
        /// <param name="p_ckBox"></param>
        /// <returns></returns>
        public static int GetCheckedItemNum(CheckBoxList p_ckBox)
        {
            int iCount = 0;
            foreach (ListItem li in p_ckBox.Items)
            {
                if (li.Selected)
                {
                    iCount++;
                }//if (li.Selected )
            }//foreach (ListItem li in p_ckBox.Items )

            return iCount;
        }

        /// <summary>
        /// 把CheckBoxList中的所有项目的选中状态设为p_value
        /// </summary>
        /// <param name="p_ckBox"></param>
        /// <param name="p_value">true  false</param>
        public static void SetCheckBoxValue(CheckBoxList p_ckBox, bool p_value)
        {
            foreach (ListItem li in p_ckBox.Items)
            {
                li.Selected = p_value;
            }//foreach (ListItem li in p_ckBox.Items )
        }



        /// <summary>
        /// 根据条件语句把传入的表的记录筛选出来，组成一个结构相同的表传出来
        /// </summary>
        /// <param name="p_dt">要筛选记录的表</param>
        /// <param name="p_Select">条件语句</param>
        /// <returns>复制后的表，里面存储被筛选出的记录</returns>
        public static DataTable CopyTable(DataTable p_dt, string p_Select)
        {
            #region 代码实现
            DataTable cloneTab = null;
            try
            {
                if ((p_Select != null) && (p_Select.ToString().Length > 0))
                {
                    cloneTab = p_dt.Clone();
                    DataRow[] dRows = p_dt.Select(p_Select);
                    int rowCount = dRows.Length;
                    for (int i = 0; i < rowCount; i++)
                    {
                        cloneTab.ImportRow(dRows[i]);
                    }
                }
                else
                    cloneTab = p_dt;  //没有设置查询条件的话就返回原表
            }
            catch
            {
                cloneTab = null;  //发生错误的话就返回空
            }
            return cloneTab;

            #endregion 代码实现
        }

        /// <summary>
        /// 根据p_dt中的数据填充成一个CheckBoxList
        /// </summary>
        /// <param name="p_dt">数据源 第一列对应TEXT，第二列对应VALUE，第一列确定是否选中(1为选中，其他为不选中)</param>
        /// <returns>填充成的CheckBoxList</returns>
        public static void FillCheckBoxListFromTable(ref CheckBoxList p_ckl, DataTable p_dt)
        {
            #region 代码实现

            p_ckl.Items.Clear();
            ListItem li = null;
            string strText = "", strValue = "", strChecked = "";
            foreach (DataRow drow in p_dt.Rows)
            {
                strText = drow[0].ToString();
                strValue = drow[1].ToString();
                strChecked = drow[2].ToString();
                li = new ListItem(strText, strValue);
                if (strChecked == "1")
                    li.Selected = true;
                else
                    li.Selected = false;
                p_ckl.Items.Add(li);

            }//foreach (DataRow drow in p_dt.Rows )

            #endregion 代码实现
        }

        /// <summary>
        /// 根据checkboxlist中的数据填充数据表
        /// </summary>
        /// <param name="p_ckl">checkboxlist</param>
        /// <returns>返回数据表</returns>
        public static DataTable GetTableFromCheckBoxList(CheckBoxList p_ckl)
        {
            #region 代码实现
            DataTable dt = new DataTable();
            DataColumn dcol = null;
            dcol = new DataColumn("Text", typeof(string));
            dt.Columns.Add(dcol);

            dcol = new DataColumn("Value", typeof(string));
            dt.Columns.Add(dcol);
            dcol = new DataColumn("Selected", typeof(string));
            dt.Columns.Add(dcol);

            string strText = "", strValue = "", strChecked = "";
            foreach (ListItem li in p_ckl.Items)
            {
                strText = li.Text;
                strValue = li.Value;
                if (li.Selected)
                    strChecked = "1";
                else
                    strChecked = "0";
                DataRow drow = dt.NewRow();
                drow[0] = strText;
                drow[1] = strValue;
                drow[2] = strChecked;
                dt.Rows.Add(drow);

            }//foreach (ListItem li in p_ckl.Items )
            return dt;
            #endregion 代码实现
        }


        /// <summary>
        ///  把p_sql查询语句对应的BLOB数据保存到磁盘文件p_file中
        /// </summary>
        /// <param name="p_sql">第一个字段就是BLOB的查询语句，查询结果只有一条记录</param>
        /// <param name="p_file">把BLOB输入保存到这个文件。带绝对路径</param>
        //public static void DownBlobToFile(string p_sql, string p_file)
        //{
        //    #region 代码实现
        //    OraDataService db = new OraDataService();
        //    OracleDataReader dreader = db.GetDataReader(p_sql);
        //    dreader.Read();
        //    FileStream fs;
        //    BinaryWriter bw;
        //    int bufferSize = 100;                   // Size of the BLOB buffer.
        //    byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
        //    long retval;                            // The bytes returned from GetBytes.
        //    long startIndex = 0;                    // The starting position in the BLOB output.
        //    fs = new FileStream(p_file, FileMode.Create, FileAccess.Write);
        //    bw = new BinaryWriter(fs);

        //    // Reset the starting byte for the new BLOB.
        //    startIndex = 0;

        //    // Read the bytes into outbyte[] and retain the number of bytes returned.
        //    retval = dreader.GetBytes(0, startIndex, outbyte, 0, bufferSize);

        //    // Continue reading and writing while there are bytes beyond the size of the buffer.
        //    while (retval == bufferSize)
        //    {
        //        bw.Write(outbyte);
        //        bw.Flush();

        //        // Reposition the start index to the end of the last buffer and fill the buffer.
        //        startIndex += bufferSize;
        //        retval = dreader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
        //    }

        //    // Write the remaining buffer.
        //    try
        //    {
        //        if ((int)retval > 1)
        //        {
        //            bw.Write(outbyte, 0, (int)retval - 1);
        //            bw.Flush();
        //        }
        //    }
        //    catch                 
        //    {
        //        dreader.Close();
        //    }
        //    // Close the output file.
        //    bw.Close();
        //    fs.Close();
        //    dreader.Dispose();

        //    #endregion 代码实现
        //}

        /// <summary>
        /// 把表示日期的字符串转化为标准的日期格式yyyy-mm-dd
        /// 例如 1999-2-3 返回1999-02-03
        /// </summary>
        /// <param name="p_Date">表示日期的字符串</param>
        /// <returns>标准的日期格式yyyy-mm-dd</returns>
        public static string GetDateString(string p_Date)
        {
            #region 代码实现
            if (p_Date.Length == 0)
                return "";
            char strSeq;
            if (p_Date.IndexOf("-", 0) >= 0)  //型如1999-12-12的日期
                strSeq = '-';
            else if (p_Date.IndexOf(".", 0) >= 0)//型如1999.12.12的日期
                strSeq = '.';
            else if (p_Date.IndexOf("/", 0) >= 0) //型如1999/12/12的日期
                strSeq = '/';
            else if (p_Date.Length == 8)  //型如19991212的日期
                return p_Date.Substring(0, 4) + "-" + p_Date.Substring(4, 2) + "-" + p_Date.Substring(6, 2);
            else
                return "";
            string[] split = p_Date.Split(strSeq);
            if (split.Length != 3)
                return "";
            else
                return split[0] + "-" + split[1].PadLeft(2, '0') + "-" + split[2].PadLeft(2, '0');  //月 天 如果是一位的都补足两位
            #endregion 代码实现
        }//public static string GetDateString(string p_Date)


        public static DateTime? GetDateNull(DateTime p_Date)
        {
            string strDate = p_Date.ToShortDateString();
            if (strDate.Substring(0, 4) == "0001")
                return null;
            else
                return p_Date;
        }

        /// <summary>
        /// 从DropDownList中取出每个listitem的text和 value，组成一个字符串返回
        /// </summary>
        /// <param name="p_ddl"></param>
        /// <returns>型如：等于@=#大于@>#小于<   </returns>
        public static string GetStrFromddl(DropDownList p_ddl)
        {
            #region 代码实现
            if (p_ddl.Items.Count == 0)
                return "";
            string strReturn = "";
            foreach (ListItem li in p_ddl.Items)
            {
                strReturn += li.Text + "@" + li.Value + "#";
            }
            strReturn = strReturn.Substring(0, strReturn.Length - 1);
            return strReturn;
            //public static string GetStrFromddl(DropDownList p_ddl)

            #endregion 代码实现
        }


        /// <summary>
        /// 相当于Char函数,根据ascii值得到对应字符
        /// </summary>
        /// <param name="p_Value"></param>
        /// <returns></returns>
        public static string getChar(int p_Value)
        {
            string strReturn = "";
            Byte[] bytes;
            string chars = "a";
            ASCIIEncoding ascii = new ASCIIEncoding();
            int byteCount = ascii.GetByteCount(chars.ToCharArray());
            bytes = new Byte[byteCount];
            int bytesEncodedCount = ascii.GetBytes(chars, 0, 1, bytes, 0);
            bytes[0] = (byte)p_Value;

            strReturn = ascii.GetString(bytes);
            return strReturn;
        }

        /// <summary>
        /// 相当于Ascii函数,根据字符得到对应的Ascii码
        /// </summary>
        /// <param name="p_Value"></param>
        /// <returns></returns>
        public static int getAscii(string p_Value)
        {
            int iReturn = 0;
            ASCIIEncoding ascii = new ASCIIEncoding();
            Byte[] encodedBytes = ascii.GetBytes(p_Value);
            iReturn = Convert.ToInt32(encodedBytes[0].ToString());
            return iReturn;

        }

        /// <summary>
        /// 把字符串"a,b,c,d"替换成"('a','b','c','d')"
        /// </summary>
        /// <param name="strlist"></param>
        /// <returns></returns>
        public static string GetPstr(string strlist)
        {
            #region 代码实现
            if (strlist == "")
                return "0";
            string[] split = strlist.Split(new char[] { ',', ' ', '，' });
            string strReturn = null;
            strReturn = "(";
            foreach (string s in split)
            {
                strReturn += "'" + s + "',";
            }
            if (strReturn.Length > 0)
                strReturn = strReturn.Substring(0, strReturn.Length - 1);
            strReturn += ")";
            return strReturn;
            #endregion 代码实现
        }

        /// <summary>
        /// 把字符串"a,b,c,d"替换成"(jh like '%a%' or jh like '%b%' or jh like '%c...)"
        /// </summary>
        /// <param name="strField"></param>
        /// <param name="strlist"></param>
        /// <returns></returns>
        public static string GetLikeStr(string strField, string strlist)
        {
            #region 代码实现
            if (strlist == "")
                return "0";
            string[] split = strlist.Split(new char[] { ',', ' ', '，' });
            string strReturn = null;
            strReturn = "";
            foreach (string s in split)
            {
                if (strReturn == "")
                    strReturn = strField + " like '%" + s + "%' ";
                else
                    strReturn += "or " + strField + " like '%" + s + "%' ";
            }
            strReturn = "(" + strReturn + ")";
            return strReturn;
            #endregion 代码实现
        }

        /// <summary>
        /// 返回大于或等于两整数相除结果的最小整数。
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public static int getCeiling(int p1, int p2)
        {
            return Convert.ToInt32(System.Math.Ceiling(Convert.ToDouble(p1) / Convert.ToDouble(p2)));
        }

        /// <summary>
        /// 把EXCEL中，列索引数字转化为 列名称。例如：1转为A  26转为Z  27转为AA
        /// 本函数只支持A到ZZ范围内的转化
        /// </summary>
        /// <param name="p_ColIndex"></param>
        /// <returns></returns>
        public static string colTag(int p_ColIndex)
        {

            #region 代码实现
            int iValue = p_ColIndex;
            int i1 = (iValue - 1) / 26;
            int i2 = (iValue) % 26;
            if (i2 == 0)
                i2 = 26;
            string str1 = getChar(i1 + 64);
            string str2 = getChar(i2 + 64);
            string ss = "";
            if (iValue <= 26)
                ss = str2;
            else
                ss = str1 + str2;
            return ss;
            #endregion 代码实现
        }

        /// <summary>
        /// 为下拉列表添加年度，从今年算起，往上推10年
        /// </summary>
        /// <param name="p_List"></param>
        public static void FillYearList(DropDownList p_List)
        {
            int iND = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
            p_List.Items.Clear();
            for (int i = 0; i < 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = (iND - i).ToString();
                li.Text = li.Value;
                p_List.Items.Add(li);
            }
        }

        /// <summary>
        /// 为下拉列表添加月份，并默认显示当前月
        /// </summary>
        /// <param name="p_List"></param>
        public static void FillMonthList(DropDownList p_List)
        {
            p_List.Items.Clear();
            int iYD = Convert.ToInt32(DateTime.Now.ToString("MM"));
            for (int i = 1; i <= 12; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = li.Value;
                p_List.Items.Add(li);
                if (i == iYD)
                    li.Selected = true;
            }
        }

        /// <summary>
        /// 库中的井号是CHAR（16）型的。如果有一个井号是 腰深1 ，从库中取出到C#代码中后它的长度是14而不是16
        /// 因为两个汉字多占了两个位置（一个汉字对应两个字符）。如果你想在C#代码中拼出一个和库中一样的井号，
        /// " 腰深1".PadRight(16,' ')是不行的，比从库中取的井号长了2个字符。应当用" 腰深1".PadRight(14,' ')
        /// 本函数的作用就是检查汉字的个数，从而拼成一个和库中一样的值
        /// </summary>
        /// <param name="p_Value">要拼凑的值，例如  "腰深1"</param>
        /// <param name="p_Length">库中该字段的CHAR型的长度</param>
        /// <param name="p_char">右边拼凑的字符，例如空格</param>
        /// <returns>拼凑后的值，例如  "腰深1      "</returns>
        public static string CharPadRight(string p_Value, int p_Length, char p_char)
        {
            int iLenB = System.Text.Encoding.GetEncoding("GB2312").GetByteCount(p_Value);
            iLenB = iLenB - p_Value.Length;
            string str = p_Value.PadRight(p_Length - iLenB, p_char);
            return str;

        }

        /// <summary>
        /// 得到两个数相除的百分数值
        /// </summary>
        /// <param name="p_1">分子</param>
        /// <param name="p_2">分母</param>
        /// <param name="p_Point">保留的小数位数</param>
        /// <returns></returns>
        public static string GetBL(object p_1, object p_2, int p_Point)
        {
            if ((p_1 == null) || (p_2 == null) || (p_2.ToString() == "") || (p_2.ToString() == "0") || (p_1.ToString() == ""))
                return "";
            decimal dP_1 = 0, dP_2 = 0;
            if (!(decimal.TryParse(p_1.ToString(), out dP_1)))
                return "";
            if (!(decimal.TryParse(p_2.ToString(), out dP_2)))
                return "";
            return Math.Round(dP_1 / dP_2 * 100, p_Point).ToString();
        }


        #region 调用存储过程时，需要传参数，这里的函数负责生成参数对象


        /// <summary>
        /// 调用存储过程时，需要传参数，本函数就是生成参数对象
        /// </summary>
        /// <param name="p_Name1">参数名称，形如  "@P_USERCODE"</param>
        /// <param name="p_Value1">参数值</param>
        /// <param name="p_Mode1">"INPUT"  OR  "OUTPUT"</param>
        /// <returns></returns>
        public static ArrayList[] getProcedureParameter(string p_Name1, object p_Value1, string p_Mode1)
        {
            #region 代码实现
            ArrayList[] alist = new ArrayList[3];

            ArrayList listName = new ArrayList();
            listName.Add(p_Name1);

            ArrayList listValue = new ArrayList();
            listValue.Add(p_Value1);

            ArrayList listMode = new ArrayList();
            listMode.Add(p_Mode1);

            alist[0] = listName;
            alist[1] = listValue;
            alist[2] = listMode;
            return alist;

            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_Name1"></param>
        /// <param name="p_Value1"></param>
        /// <param name="p_Mode1"></param>
        /// <param name="p_Name2"></param>
        /// <param name="p_Value2"></param>
        /// <param name="p_Mode2"></param>
        /// <returns></returns>
        public static ArrayList[] getProcedureParameter(
            string p_Name1, object p_Value1, string p_Mode1,
            string p_Name2, object p_Value2, string p_Mode2)
        {
            #region 代码实现
            ArrayList[] alist = new ArrayList[3];

            ArrayList listName = new ArrayList();
            listName.Add(p_Name1);
            listName.Add(p_Name2);

            ArrayList listValue = new ArrayList();
            listValue.Add(p_Value1);
            listValue.Add(p_Value2);

            ArrayList listMode = new ArrayList();
            listMode.Add(p_Mode1);
            listMode.Add(p_Mode2);

            alist[0] = listName;
            alist[1] = listValue;
            alist[2] = listMode;
            return alist;
            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_Name1"></param>
        /// <param name="p_Value1"></param>
        /// <param name="p_Mode1"></param>
        /// <param name="p_Name2"></param>
        /// <param name="p_Value2"></param>
        /// <param name="p_Mode2"></param>
        /// <param name="p_Name3"></param>
        /// <param name="p_Value3"></param>
        /// <param name="p_Mode3"></param>
        /// <returns></returns>
        public static ArrayList[] getProcedureParameter(
           string p_Name1, object p_Value1, string p_Mode1,
           string p_Name2, object p_Value2, string p_Mode2,
           string p_Name3, object p_Value3, string p_Mode3)
        {

            #region 代码实现
            ArrayList[] alist = new ArrayList[3];

            ArrayList listName = new ArrayList();
            listName.Add(p_Name1);
            listName.Add(p_Name2);
            listName.Add(p_Name3);

            ArrayList listValue = new ArrayList();
            listValue.Add(p_Value1);
            listValue.Add(p_Value2);
            listValue.Add(p_Value3);

            ArrayList listMode = new ArrayList();
            listMode.Add(p_Mode1);
            listMode.Add(p_Mode2);
            listMode.Add(p_Mode3);

            alist[0] = listName;
            alist[1] = listValue;
            alist[2] = listMode;
            return alist;

            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_Name1"></param>
        /// <param name="p_Value1"></param>
        /// <param name="p_Mode1"></param>
        /// <param name="p_Name2"></param>
        /// <param name="p_Value2"></param>
        /// <param name="p_Mode2"></param>
        /// <param name="p_Name3"></param>
        /// <param name="p_Value3"></param>
        /// <param name="p_Mode3"></param>
        /// <param name="p_Name4"></param>
        /// <param name="p_Value4"></param>
        /// <param name="p_Mode4"></param>
        /// <returns></returns>
        public static ArrayList[] getProcedureParameter(
           string p_Name1, object p_Value1, string p_Mode1,
           string p_Name2, object p_Value2, string p_Mode2,
           string p_Name3, object p_Value3, string p_Mode3,
            string p_Name4, object p_Value4, string p_Mode4)
        {

            #region 代码实现
            ArrayList[] alist = new ArrayList[4];

            ArrayList listName = new ArrayList();
            listName.Add(p_Name1);
            listName.Add(p_Name2);
            listName.Add(p_Name3);
            listName.Add(p_Name4);

            ArrayList listValue = new ArrayList();
            listValue.Add(p_Value1);
            listValue.Add(p_Value2);
            listValue.Add(p_Value3);
            listValue.Add(p_Value4);

            ArrayList listMode = new ArrayList();
            listMode.Add(p_Mode1);
            listMode.Add(p_Mode2);
            listMode.Add(p_Mode3);
            listMode.Add(p_Mode4);

            alist[0] = listName;
            alist[1] = listValue;
            alist[2] = listMode;
            return alist;

            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_Name1"></param>
        /// <param name="p_Value1"></param>
        /// <param name="p_Mode1"></param>
        /// <param name="p_Name2"></param>
        /// <param name="p_Value2"></param>
        /// <param name="p_Mode2"></param>
        /// <param name="p_Name3"></param>
        /// <param name="p_Value3"></param>
        /// <param name="p_Mode3"></param>
        /// <param name="p_Name4"></param>
        /// <param name="p_Value4"></param>
        /// <param name="p_Mode4"></param>
        /// <param name="p_Name5"></param>
        /// <param name="p_Value5"></param>
        /// <param name="p_Mode5"></param>
        /// <returns></returns>
        public static ArrayList[] getProcedureParameter(
   string p_Name1, object p_Value1, string p_Mode1,
   string p_Name2, object p_Value2, string p_Mode2,
   string p_Name3, object p_Value3, string p_Mode3,
    string p_Name4, object p_Value4, string p_Mode4,
            string p_Name5, object p_Value5, string p_Mode5)
        {

            #region 代码实现
            ArrayList[] alist = new ArrayList[5];

            ArrayList listName = new ArrayList();
            listName.Add(p_Name1);
            listName.Add(p_Name2);
            listName.Add(p_Name3);
            listName.Add(p_Name4);
            listName.Add(p_Name5);

            ArrayList listValue = new ArrayList();
            listValue.Add(p_Value1);
            listValue.Add(p_Value2);
            listValue.Add(p_Value3);
            listValue.Add(p_Value4);
            listValue.Add(p_Value5);

            ArrayList listMode = new ArrayList();
            listMode.Add(p_Mode1);
            listMode.Add(p_Mode2);
            listMode.Add(p_Mode3);
            listMode.Add(p_Mode4);
            listMode.Add(p_Mode5);

            alist[0] = listName;
            alist[1] = listValue;
            alist[2] = listMode;
            return alist;

            #endregion 代码实现
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_Name1"></param>
        /// <param name="p_Value1"></param>
        /// <param name="p_Mode1"></param>
        /// <param name="p_Name2"></param>
        /// <param name="p_Value2"></param>
        /// <param name="p_Mode2"></param>
        /// <param name="p_Name3"></param>
        /// <param name="p_Value3"></param>
        /// <param name="p_Mode3"></param>
        /// <param name="p_Name4"></param>
        /// <param name="p_Value4"></param>
        /// <param name="p_Mode4"></param>
        /// <param name="p_Name5"></param>
        /// <param name="p_Value5"></param>
        /// <param name="p_Mode5"></param>
        /// <param name="p_Name6"></param>
        /// <param name="p_Value6"></param>
        /// <param name="p_Mode6"></param>
        /// <returns></returns>
        public static ArrayList[] getProcedureParameter(
  string p_Name1, object p_Value1, string p_Mode1,
  string p_Name2, object p_Value2, string p_Mode2,
  string p_Name3, object p_Value3, string p_Mode3,
   string p_Name4, object p_Value4, string p_Mode4,
           string p_Name5, object p_Value5, string p_Mode5,
             string p_Name6, object p_Value6, string p_Mode6)
        {

            #region 代码实现
            ArrayList[] alist = new ArrayList[6];

            ArrayList listName = new ArrayList();
            listName.Add(p_Name1);
            listName.Add(p_Name2);
            listName.Add(p_Name3);
            listName.Add(p_Name4);
            listName.Add(p_Name5);
            listName.Add(p_Name6);

            ArrayList listValue = new ArrayList();
            listValue.Add(p_Value1);
            listValue.Add(p_Value2);
            listValue.Add(p_Value3);
            listValue.Add(p_Value4);
            listValue.Add(p_Value5);
            listValue.Add(p_Value6);

            ArrayList listMode = new ArrayList();
            listMode.Add(p_Mode1);
            listMode.Add(p_Mode2);
            listMode.Add(p_Mode3);
            listMode.Add(p_Mode4);
            listMode.Add(p_Mode5);
            listMode.Add(p_Mode6);

            alist[0] = listName;
            alist[1] = listValue;
            alist[2] = listMode;
            return alist;

            #endregion 代码实现
        }


        #endregion



        /// <summary>
        /// 发送邮件函数
        /// </summary>
        /// <param name="SmtpServer">smtp服务器 比如："smtp.163.com"</param>
        /// <param name="PortNo">smtp服务器端口号： 默认 25</param>
        /// <param name="UserName">smtp服务器用户名 比如：zhaoxianfa</param>
        /// <param name="PassWord">smtp服务器用户密码： 比如： xxxxx</param>
        /// <param name="Sender">发件人邮箱地址 比如：zhaoxianfa@163.com</param>
        /// <param name="Receiver">收件人邮箱地址 比如： xxx@163.com</param>
        /// <param name="Subject">邮件标题 比如： 本月工资条 </param>
        /// <param name="Content">邮件内容 比如： 实发工资20000</param>
        /// <param name="FileList">发送文件列表 内容为文件的服务器物理绝对路径</param>
        /// <returns>成功返回true，否则返回false</returns>
        public static bool SendMail(string SmtpServer, int PortNo, string UserName, string PassWord, string Sender, string Receiver, string Subject, string Content, System.Collections.Generic.List<string> FileList)
        {
            try
            {
                System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage();
                myMail = new System.Net.Mail.MailMessage(Sender, Receiver, Subject, Content);
                if (FileList != null)
                {
                    for (int i = 0; i < FileList.Count; i++)
                    {
                        string file = FileList[i].ToString();
                        System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
                        System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
                        disposition.CreationDate = System.IO.File.GetCreationTime(file);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
                        myMail.Attachments.Add(myAttachment);
                    }
                }
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(SmtpServer, PortNo);
                client.Credentials = new System.Net.NetworkCredential(UserName, PassWord);

                client.Send(myMail);
                return true;
            }
            catch
            {
                return false;
            }
        }


    }
}

